﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using FigKey_TestExec_SL.Common;

namespace Test_Operator_Platform.Common
{
    class DataViewUtils
    {
        public const string COL_NUMBER = "Number";
        public const string COL_TESTPLAN = "Test Plan";
        public const string COL_TEST_ITEM = "Test Item";
        public const string COL_LIMIT_MIN = "Limit Minimum";
        public const string COL_LIMIT_MAX = "Limit Maximum";
        public const string COL_UNIT = "Unit";
        public const string COL_VALUE = "Test Value";
        public const string COL_RESULT = "Test Result";
        public const string COL_START_DATE = "Start Date";
        public const string COL_END_DATE = "End Date";

        public static void InitDataGrid(DataGridView view)
        {
            view.Columns.Add("colNumber", "Number");//测试序列
            view.Columns.Add("colItem", "Test Item");//测试项
            view.Columns.Add("colAction", "Test Action");
            view.Columns.Add("colMin", "Limit Minimum");//规格下限
            view.Columns.Add("colMax", "Limit Maximum");//规格上限
            view.Columns.Add("colUnit", "Unit");//单位
            view.Columns.Add("colValue", "Test Value");//测试值
            view.Columns.Add("colStatus", "Test Result");//测试状态

            for (int i = 0; i < view.Columns.Count; i++)
            {
                view.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            view.AllowUserToAddRows = false;
            view.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //view.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            view.MultiSelect = false;
            view.ReadOnly = true;
            view.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            view.Font = new Font("宋体", 12f);

            //set define column width
            SetGridAdapter(view);
            view.Columns[5].Visible = false;

            var sequence = GetTestSequence();
            if (sequence == "0")
            {
                view.Columns[2].Visible = false;
            }
        }

        public static void SetGridView(DataGridView view)
        {
            for (int i = 0; i < view.Columns.Count; i++)
            {
                view.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            view.AllowUserToAddRows = false;
            view.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //view.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            view.MultiSelect = false;
            view.ReadOnly = true;
            view.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            view.Font = new Font("宋体", 12f);
            view.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        public static void SetGridAdapter(DataGridView view)
        {
            view.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            view.Columns[0].Width -= 35;
            view.Columns[3].Width -= 35;
            view.Columns[4].Width -= 35;
            view.Columns[6].Width -= 25;
        }

        public static void InitTestDataGrid(DataGridView view)
        {
            view.Columns.Add("col1", COL_NUMBER);//测试序列
            view.Columns.Add("col2", COL_TESTPLAN);
            view.Columns.Add("col3", COL_TEST_ITEM);//测试项
            view.Columns.Add("col4", COL_LIMIT_MIN);//规格下限
            view.Columns.Add("col5", COL_LIMIT_MAX);//规格上限
            view.Columns.Add("col6", COL_UNIT);//单位
            view.Columns.Add("col7", COL_VALUE);//测试值
            view.Columns.Add("col8", COL_RESULT);//测试状态

            for (int i = 0; i < view.Columns.Count; i++)
            {
                view.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            view.AllowUserToAddRows = false;
            view.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            view.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            view.MultiSelect = false;
            view.ReadOnly = true;

            //set define column width
            view.Columns[0].Width = 120;
            view.Columns[5].Width = 80;
            view.Columns[7].Width = 120;
            view.Columns[5].Visible = false;
        }

        public static void InitTestResultGrid(DataGridView view)
        {
            view.Columns.Add("col1", COL_NUMBER);//测试序列
            view.Columns.Add("col2", COL_TESTPLAN);
            view.Columns.Add("col3", COL_TEST_ITEM);//测试项
            view.Columns.Add("col4", COL_LIMIT_MIN);//规格下限
            view.Columns.Add("col5", COL_LIMIT_MAX);//规格上限
            view.Columns.Add("col6", COL_UNIT);//单位
            view.Columns.Add("col7", COL_VALUE);//测试值
            view.Columns.Add("col8", COL_RESULT);//测试状态

            for (int i = 0; i < view.Columns.Count; i++)
            {
                view.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            view.AllowUserToAddRows = false;
            view.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            view.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            view.MultiSelect = false;
            view.ReadOnly = true;

            //set define column width
            view.Columns[0].Width = 120;
            view.Columns[5].Width = 80;
            view.Columns[7].Width = 120;
            view.Columns[5].Visible = false;
        }

        public static System.Data.DataTable GetTestTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add(COL_NUMBER);
            //dt.Columns.Add(COL_TESTPLAN);
            dt.Columns.Add(COL_TEST_ITEM);
            dt.Columns.Add(COL_LIMIT_MIN);
            dt.Columns.Add(COL_LIMIT_MAX);
            //dt.Columns.Add(COL_UNIT);
            dt.Columns.Add(COL_VALUE);
            dt.Columns.Add(COL_RESULT);
            dt.Columns.Add(COL_START_DATE);
            dt.Columns.Add(COL_END_DATE);
            return dt;
        }

        public static void SetSingleTabPageShow(TabPage tg, TabControl tb)
        {
            foreach (TabPage tabPage in tb.TabPages)
            {
                if (tabPage != tg)
                    tabPage.Parent = null;
            }
            tg.Parent = tb;
            tb.SizeMode = TabSizeMode.Fixed;
            tb.ItemSize = new Size(1, 1);
        }

        public static string GetTestSequence()
        {
            var sequence = System.Configuration.ConfigurationManager.AppSettings["testSequence"];
            if (sequence == null)
                return "0";
            return sequence.ToString();
        }
    }
}
